package com.zuche.seraph.system.service.impl;

import com.zuche.seraph.system.entity.AppBean;
import com.zuche.seraph.system.dao.AppDao;
import com.zuche.seraph.system.service.AppService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SeraphApp)表服务实现类
 *
 * @author makejava
 * @since 2021-08-29 21:07:13
 */
@Service("seraphAppService")
public class AppServiceImpl implements AppService {

    @Resource
    private AppDao appDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AppBean queryById(Integer id) {
        return this.appDao.queryById(id);
    }

    @Override
    public AppBean getAppServerByIp(String ip) {
        List<AppBean>  list = this.appDao.queryAll(null);
        for (AppBean item : list){
            if(item.getIpList()!=null && item.getIpList().contains(ip)){
                return item;
            }
        }
        return null;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<AppBean> queryAllByLimit(int offset, int limit) {
        return this.appDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param appBean 实例对象
     * @return 实例对象
     */
    @Override
    public AppBean insert(AppBean appBean) {
        this.appDao.insert(appBean);
        return appBean;
    }

    /**
     * 修改数据
     *
     * @param appBean 实例对象
     * @return 实例对象
     */
    @Override
    public AppBean update(AppBean appBean) {
        this.appDao.update(appBean);
        return this.queryById(appBean.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.appDao.deleteById(id) > 0;
    }

    @Override
    public List<AppBean> getAppServerByAppName(String appName) {
        AppBean param = new AppBean();
        param.setAppName(appName);
        List<AppBean>  list = this.appDao.queryAll(param);
        if(list!=null && list.size()>0){
            return list;
        }
        return null;
    }
}
